package com.ynet.hmds28.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.taobao.middleware.logger.Logger;
import com.taobao.middleware.logger.LoggerFactory;
import com.ynet.hm.merchant.domain.ContentBean;
import com.ynet.hm.movie.domain.MovieListBean;
import com.ynet.hm.utils.HttpHelper;
import com.ynet.hm.utils.UuidUtil;
import com.ynet.hm.utils.config.Result;
import com.ynet.hm.utils.config.ResultUtil;
import com.ynet.hm.utils.enums.ExceptionEnum;
import com.ynet.hm.utils.exception.MyException;
import com.ynet.hm.utils.rsa.RSAHelper;
import com.ynet.hmds28.redis.RedisService;

@RestController
public class MovieQueryController {

	private static final Logger logger = LoggerFactory
			.getLogger(MovieQueryController.class);

	@Autowired
	private RedisService redisService;
	
	// 查询url
	@Value("${queryUrl}")
	public String queryUrl;

	/**
	 * 根据城市代码查询天气指数
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/api/movie/query", method = RequestMethod.POST)
	public Result<?> queryAir(HttpServletRequest request) {

		// 商户号
//		String merchantNo = request.getParameter("merchantNo");
		String merchantNo = "fx001";
		// 电影院id
		String cinemaId = request.getParameter("cinemaId");
		// 使用的服务id
		String provideId = request.getParameter("provideId");
		// 交易流水号
		String flow = UuidUtil.getUUid();
		//商户私钥
		String merchantPrivateKey = redisService.get(merchantNo+"privateKey").toString();
		//平台公钥
		String platformPublicKey = redisService.get("platFormPublicKey").toString();
		ContentBean movieBean = new ContentBean();

		movieBean.setContent(cinemaId);
		movieBean.setFlow(flow);
		movieBean.setMerchantNo(merchantNo);
		movieBean.setProvideId(provideId);

		String moviebeanStr = JSONObject.toJSONString(movieBean);
		// 使用商户私钥签名
		logger.info("使用商户私钥签名");
		String strBody = RSAHelper.sign(moviebeanStr, merchantPrivateKey);

		Map<String, String> data = new HashMap<String, String>();
		data.put("strBody", strBody);
		data.put("oldstrBody", moviebeanStr);
		try {
			JSONObject jsondata = HttpHelper.httpPost(queryUrl, data);

			JSONObject resultBody = jsondata.getJSONObject("data");

			String oldAirStr = resultBody.getString("oldAirStr");
			String airStr = resultBody.getString("airStr");
			// 使用平台公钥验签
			logger.info("使用平台公钥验签");
			if (!RSAHelper.verify(oldAirStr, platformPublicKey, airStr)) {
				throw new MyException(ExceptionEnum.MERCHANT_IS_ERROR);
			}
			;
			logger.info("平台公钥验签成功");
			MovieListBean movieListBean = JSONObject.parseObject(oldAirStr,
					MovieListBean.class);

			return ResultUtil.success(movieListBean);
		} catch (MyException e) {
			throw new MyException(e.getCode(), e.getMessage());
		}

	}
}
